/** @file    CoderXML_Security.cpp
 *  @author  Alessandro Polo
 *  @version $Id: CoderXML_Security.cpp 2047 2010-03-07 00:27:16Z alex $
 *  @brief
 * File containing methods for Serialization.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <framework/database/CoderXML_Security.h>
 #include <framework/database/CoderXML_Core.h>
 #include <framework/database/EncoderXML.h>


using namespace std;
using namespace wosh::security;
namespace wosh {
 namespace database {

void EncoderXML_register_Security()
 {
 //_AlarmEvent_TYPE
	EncoderXML::registerDataType( _AlarmTriggerGeneric_TYPE,	new EncoderXML_template__AlarmTriggerGeneric() );
	EncoderXML::registerDataType( _AlarmTriggerPerimetral_TYPE,	new EncoderXML_template__AlarmTriggerPerimetral() );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderXML_template__AlarmTrigger::serializeTo( const ISerializable* object_s, QDomDocument& doc, QDomElement& contNode, bool compact )
 {
	if ( object_s == NULL ) return WRET_ERR_PARAM;
	const wosh::security::AlarmTrigger* object = dynamic_cast<const wosh::security::AlarmTrigger*>(object_s);
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;

	if ( compact ) {
		ret += EncoderXML_template__Object().serializeTo( object, doc, contNode, true );

	 }
	else {
		QDomElement objectNode = doc.createElement("object");
		contNode.appendChild(objectNode);
		ret += EncoderXML_template__Object().serializeTo( object, doc, objectNode, false );

	 }

	if ( object->isEnabled() )
		contNode.setAttribute("enabled", "true" );
	else
		contNode.setAttribute("enabled", "false" );

	QDomElement levelNode = doc.createElement("level");
	contNode.appendChild(levelNode);
	if ( compact ) {
		levelNode.setAttribute("multiplier", QString::number(object->getAlarmLevelMultiplier()) );
		levelNode.setAttribute("reference", QString::number(object->getAlarmLevelRef()) );
	 }
	else {
		QDomElement multiplierNode = doc.createElement("multiplier");
		multiplierNode.appendChild( doc.createTextNode( QString::number(object->getAlarmLevelMultiplier()) ) );
		levelNode.appendChild(multiplierNode);

		QDomElement referenceNode = doc.createElement("reference");
		referenceNode.appendChild( doc.createTextNode( QString::number(object->getAlarmLevelRef()) ) );
		levelNode.appendChild(referenceNode);
	 }

	QDomElement descNode = doc.createElement("description");
	descNode.appendChild( doc.createTextNode( object->getDescription().c_str() ) );
	levelNode.appendChild(descNode);

	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderXML_template__AlarmTrigger::deserializeFrom( ISerializable* object_s, const QDomElement& contNode )
 {
 	if ( object_s == NULL ) return WRET_ERR_PARAM;
	wosh::security::AlarmTrigger* object = dynamic_cast<wosh::security::AlarmTrigger*>(object_s);
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;

	QDomElement objectNode = contNode.namedItem("object").toElement();
	if ( !objectNode.isNull() )
		ret += EncoderXML_template__Object().deserializeFrom( object, objectNode );
	else
		ret += EncoderXML_template__Object().deserializeFrom( object, contNode );

	if ( contNode.attribute("enabled").toUpper() == "FALSE" )
		object->setEnabled(false);

	QDomElement levelNode = contNode.namedItem("level").toElement();
	if ( !levelNode.isNull() ) {
		QDomElement contNode_mult = levelNode.namedItem("multiplier").toElement();
		if ( contNode_mult.isNull() )
			object->setAlarmLevelMultiplier( levelNode.attribute("multiplier").toDouble() );
		else
			object->setAlarmLevelMultiplier( contNode_mult.text().toDouble() );

		QDomElement contNode_ref = levelNode.namedItem("reference").toElement();
		if ( contNode_ref.isNull() )
			object->setAlarmLevelRef( levelNode.attribute("reference").toDouble() );
		else
			object->setAlarmLevelRef( contNode_ref.text().toDouble() );
	 }

	QDomElement contNode_desc = contNode.namedItem("description").toElement();
	if ( contNode_desc.isNull() )
		object->setDescription( contNode.attribute("description").toLatin1().data() );
	else
		object->setDescription( contNode_desc.text().toLatin1().data() );

	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderXML_template__AlarmTriggerGeneric::serializeTo( const ISerializable* object_s, QDomDocument& doc, QDomElement& contNode, bool compact )
 {
	if ( object_s == NULL ) return WRET_ERR_PARAM;
	const wosh::security::AlarmTriggerGeneric* object = dynamic_cast<const wosh::security::AlarmTriggerGeneric*>(object_s);
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;

	ret = EncoderXML_template__AlarmTrigger().serializeTo( object_s, doc, contNode, compact );
	(void)object;

	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderXML_template__AlarmTriggerGeneric::deserializeFrom( ISerializable* object_s, const QDomElement& contNode )
 {
 	if ( object_s == NULL ) return WRET_ERR_PARAM;
	wosh::security::AlarmTriggerGeneric* object = dynamic_cast<wosh::security::AlarmTriggerGeneric*>(object_s);
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;

	ret = EncoderXML_template__AlarmTrigger().deserializeFrom( object_s, contNode );

	EncoderXML_template__MessageFilter msgFilterEncoder;
	QDomElement triggers = contNode.namedItem("triggers").toElement();
	if ( !triggers.isNull() ) {
		QDomNodeList nodesList = triggers.childNodes();
		for( unsigned int j=0; j<nodesList.length(); ++j ) {
			QDomElement childNode = nodesList.item(j).toElement();
			MessageFilter * message = new MessageFilter();
			msgFilterEncoder.deserializeFrom( message , childNode );
			object->getTriggers().append(message );
		 }
	 }

	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderXML_template__AlarmTriggerPerimetral::serializeTo( const ISerializable* object_s, QDomDocument& doc, QDomElement& contNode, bool compact )
 {
	if ( object_s == NULL ) return WRET_ERR_PARAM;
	const wosh::security::AlarmTriggerPerimetral* object = dynamic_cast<const wosh::security::AlarmTriggerPerimetral*>(object_s);
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;

	ret = EncoderXML_template__AlarmTrigger().serializeTo( object_s, doc, contNode, compact );
	(void)object;

	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderXML_template__AlarmTriggerPerimetral::deserializeFrom( ISerializable* object_s, const QDomElement& contNode )
 {
 	if ( object_s == NULL ) return WRET_ERR_PARAM;
	wosh::security::AlarmTriggerPerimetral* object = dynamic_cast<wosh::security::AlarmTriggerPerimetral*>(object_s);
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = WRET_OK;

	ret = EncoderXML_template__AlarmTrigger().deserializeFrom( object_s, contNode );
	(void)object;

	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace database
}; // namespace wosh
